/**
@brief Carte:<br>
Header contenant toutes les variables et fonctions relatives a une Carte
@author Dahan Geslain , Damien Chay
@file Carte.cpp
@version 1.0
@date 2012/11/15
*/

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "Carte.h"

using namespace std;
// Mutateur

void setDim(Carte *Map, int x, int y)
{
    setDimX(&Map->Dim, x);
    setDimY(&Map->Dim, y);
}


// Accesseur

const Dimension* getDim(const Carte* Map)
{

    return &Map->Dim;

}

CaseTerrain* getCase(const Carte *Map , int x , int y)
{
    return &Map->Cases[x][y];
}


void InitMap(Carte *Map, int dimx , int dimy)
{
    setDim(Map , dimx , dimy);
    CaseTerrain** CaseT;
    Map->Cases = (CaseTerrain**) malloc(dimx * sizeof(CaseTerrain*));
    for (int i = 0; i< dimx; i++)
    {
        Map->Cases[i] = (CaseTerrain*) malloc (dimy * sizeof(CaseTerrain));
    }
}

void InitCaseMapTerrain(Carte *Map, int x , int y , unsigned int NTerrain, bool NCollision)
{
    setTerrain(&Map->Cases[x][y] , NTerrain);
    setCollision(&Map->Cases[x][y] , NCollision);
}

void InitCaseMapDecor(Carte *Map, int x , int y , unsigned int NDecor ,bool NCollision)
{
    setDecor(&Map->Cases[x][y] , NDecor);
    setCollision(&Map->Cases[x][y] , NCollision);
}

void AffichageCarte(const Carte* Map)
{
    cout <<" x = "<<getDimX(&Map->Dim)<<" y = "<<getDimY(&Map->Dim)<<endl;
    for (int i = 0; i < getDimY(&Map->Dim); i++)
    {
        for (int j = 0 ; j<getDimX(&Map->Dim); j++)
        {
            cout << Map->Cases[j][i].terrain << " ";
        }
        cout << endl;
    }
}

void TestamentCarte(Carte* Map)
{
    int dimx = getDimX(getDim(Map));

    for (int i = 0; i< dimx; i++)
    {
        free(Map->Cases[i]);
    }

    free(Map->Cases);
}
